<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Form;
use DB;

class LiaoBaseController extends Controller
{
    // 对应的模型
    protected $model;

    // 所有的字段
    protected $fields_all;

    // 列表页显示的字段
    protected $fields_show;

    // 编辑页面显示的字段
    protected $fields_edit;

    // 创建页面显示的字段
    protected $fields_create;

    public function __construct()
    {
        $model = new $this->model;
        $table = $model->table;
        // TODO:做一些基础的判断，如果没有的话就抛出异常
        $route = Route::currentRouteAction ();
        list($this->controller, $action) = explode ('@', $route);
        $this->controller = '\\' . $this->controller;
        View::share ('controller', $this->controller);

        $fields_show = array();
        foreach ($this->fields_show as $field) {
            $_field                 = $this->setdd ($table, $field);
            $fields_show[ $_field ] = $this->fields_all[ $field ];
        }
        $this->fields_show_select = $fields_show;
        //var_dump (dd(array_get($fields_show,'users_name')));
        View::share ('fields_show', $fields_show);

        $fields_edit = array();
        foreach ($this->fields_edit as $field) {
            $fields_edit[ $field ] = $this->fields_all[ $field ];
        }
        View::share ('fields_edit', $fields_edit);

        $fields_create = array();
        foreach ($this->fields_create as $field) {
            $fields_create[ $field ] = $this->fields_all[ $field ];
        }
        View::share ('fields_create', $fields_create);

        View::share ('input', Input::all ());
    }

    protected function setdd($table, $field)
    {
        if (strstr ($field, '.') === false) {
            $field = $table . '.' . $field;
        }

        return str_replace ('.', '_', $field);

    }

    public function index()
    {
        $model   = new $this->model;
        $builder = $model->orderBy ('id', 'desc');
        //dd($this->fields_show);
        $select_value = array_keys ($this->fields_show_select);
        $select_value = implode (',', $select_value);
        $builder->select(DB::raw($select_value));
        //$builder->select(DB::raw('users.id as users_id,users.name as users_name,users.email as users_email'));
        //$builder->select ('users.id as users_id');
        //$builder->addSelect ('users.name as users_name');
        //$builder->addSelect ('users.email as users_email');
        $input = Input::all ();
        foreach ($input as $field => $value) {
            if (empty($value)) {
                continue;
            }
            if (!isset($this->fields_all[ $field ])) {
                continue;
            }
            $search = $this->fields_all[ $field ];
            $builder->whereRaw ($search['search'], [$value]);
        }
        $models = $builder->paginate (20);

        return View::make ('liao.index', [
            'models' => $models,
        ]);
    }

    public function create()
    {
        return View::make ('liao.create', []);
    }

    public function store()
    {
        $model = new $this->model;
        //dd(Input::all ());
        $data       = Input::all ();
        $data['id'] = md5 (uniqid ());
        $model->fill ($data);
        $model->save ();

        return Redirect::to (action ($this->controller . '@index'));
    }

    public function edit($id)
    {
        $model  = new $this->model;
        $models = $model->find ($id);

        return View::make ('liao.edit', [
            'model' => $models,
        ]);
    }

    public function update($id)
    {
        $model = new $this->model;
        $model = $model->find ($id);
        $model->fill (Input::all ());
        $model->save ();

        return Redirect::to (action ($this->controller . '@index'));
    }

    public function destroy($id)
    {
        $model = new $this->model;
        $model->destroy ($id);

        return Redirect::to (action ($this->controller . '@index'));
    }
}
